package com.ts.api.module.app.log.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.exp.BizException;
import com.ts.api.module.channel.service.ChannelMarketService;
import com.ts.api.module.visitor.service.ChannelVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *  APP用户行为轨迹日志相关
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/app/log")
public class AppUserLogController {

    @Autowired
    private ChannelVisitorService channelVisitorService;
    @Autowired
    private ChannelMarketService marketService;

    /**
     *  插入用户点击贷超的日志
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @PostMapping("/insert/market")
    public ResultVO<Object> insertMarket(@RequestParamVo RequestVO requestVO) {
        Map<String, Object> params = requestVO.getParams();
        if (params == null) {
            BizException.fastFail(BizEnum.EMPTY);
        }

        Long marketId = requestVO.get("marketId", Long.class);
        if (marketId == null) {
            BizException.fastFail(BizEnum.EMPTY);
        }

        Long channelId = ChannelContext.getId();

        //  检测参数
        int count = marketService.countByChannelIdAndMarketId(channelId, marketId);
        if (count == 0) {
            BizException.fastFail(BizEnum.MARKET_EMPTY);
        }

        channelVisitorService.insertMarketVisitor(marketId);
        return ResultVO.suc();
    }

    /**
     *  插入用户点击机构的日志
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @PostMapping("/insert/org")
    public ResultVO<Object> insert(@RequestParamVo RequestVO requestVO) {
        Map<String, Object> params = requestVO.getParams();
        if (params == null) {
            BizException.fastFail(BizEnum.EMPTY);
        }

        Long marketId = requestVO.get("orgId", Long.class);
        if (marketId == null) {
            BizException.fastFail(BizEnum.EMPTY);
        }

        Long channelId = ChannelContext.getId();
        return ResultVO.suc();
    }

}
